package com.itzdm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzdm.reggie.common.Result.Result;
import com.itzdm.reggie.dto.SetmealDto;
import com.itzdm.reggie.entity.Dish;
import com.itzdm.reggie.entity.Setmeal;
import com.itzdm.reggie.entity.SetmealDish;
import com.itzdm.reggie.service.DishService;
import com.itzdm.reggie.service.SetmealDishService;
import com.itzdm.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;


    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @ApiOperation("添加套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> saveSetmeal(@RequestBody SetmealDto setmealDto) {
        log.info("添加套餐：{}", setmealDto.toString());
        setmealService.saveWithDish(setmealDto);
        return Result.success("添加成功");
    }

    /**
     * 套餐分页查询接口
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询接口")
    public Result<Page> page(int page, int pageSize, String name) {
        log.info("套餐分页查询：{}{}}{}", page, pageSize, name);
        Page p = setmealService.pageSet(page, pageSize, name);
        return Result.success(p);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> deleteSetmeal(@RequestParam List<Long> ids) {
        log.info("批量删除套餐：{}", ids.toString());
        setmealService.deleteWithDish(ids);
        return Result.success("删除成功");
    }

    /**
     * 启用或禁用套餐
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> startOrStop(@PathVariable Integer status,@RequestParam List<Long> ids){
        log.info("启用或禁用套餐：{}，{}", status, ids.toString());
        setmealService.startOrStop(status, ids);
        return Result.success("更新状态成功");
    }

    @GetMapping("/list")
    @ApiOperation("根据条件查询套餐数据")
    @Cacheable(cacheNames = "setmealCache",key = "#setmeal.categoryId")
    public Result<List<Setmeal>> list(Setmeal setmeal){
        log.info("查询套餐列表：{}", setmeal.toString());
        List<Setmeal> list = setmealService.getList(setmeal);
        return Result.success(list);
    }


    @GetMapping("/dish/{id}")
    public Result<List<Dish>> SetmealDishList(@PathVariable Long id){
        log.info("查看套餐包含的菜品：{}",id);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        List<Dish> list1 = list.stream().map(item -> {
            Long dishId = item.getDishId();
            return dishService.getById(dishId);
        }).toList();
        return Result.success(list1);
    }
}
